home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: I can't print the date in the format I want.
- Date: Fri, 23 Feb 96 00:35:32 GMT
- Organization: none
- Message-ID: <825035732snz@genesis.demon.co.uk>
- References: <4g5nbf$8s0@newsbf02.news.aol.com> <3129e355.114134@news.iquest.net>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <3129e355.114134@news.iquest.net>
- rclark@iquest.net "Robert B. Clark" writes:
-
- >On 17 Feb 1996 18:11:43 -0500, asciizero@aol.com (ASCII zero) wrote:
- >
- >>I am trying to use strftime() to format the date into YY-MM-DD. The format
- >>seems to be working but I am unable to actually print the formatted
- >>string. It compiles just fine using gcc. What have I overlooked?
- >
- >You need to check your compiler and make sure that you've not disabled
- >those oh-so-useful warning messages:
- >
- >>main()
- >
- >NC.
- >
- >On second thought, I'll comment anyway. Your function ought to return
- >some sort of value, if for no other reason than to reduce the flamewars
- >people tend to get into over various permutations of the main()
- >declaration:
- >
- > int main(void)
-
- Good advice but be aware that if the return type of a function
- is unspecified it defaults to int. so ``main() {}'' defines a function that
- takes no arguments and returns int.
-
- >> debug = strftime(str, 50, "The current date is %y-%m-%d", ptr);
- >
- >"Possible use of 'str' before definition."
-
- Definite misuse of terminology :-)
-
- str was defined on a previous line by char *str; . However this code certainly
- reads the value of str before setting it i.e. it passes an undefined value
- to strftime().
-
- ...
-
- >> getchar(); /* OK up to this point? */
- >
- >"Code has no effect."
- >
- >Doesn't do anything but wait for the user to press a key. If that was
- >your intent, it would be better to express this as
- >
- > if getchar();
-
- Perhaps you meant:
-
- if (getchar());
-
- However all the if statement seems to do is obfuscate the code.
-
- ...
-
- >Here is a slight revision of your code, with the disastrous null pointer
- >taken care of by explicitly declaring a char array for str instead of
- >just a char pointer:
- >
- >#include <stdio.h>
- >#include <time.h>
- >#include <string.h> /* For strlen() function */
- >#define SLEN 50 /* Allocate 50 chars for str */
- >
- >int main(void)
- >{
- > struct tm *ptr;
- > time_t lt;
- > char str[SLEN]; /* Explicitly allocate 50 chars for str */
- > int debug;
- >
- > lt = time(NULL);
- > ptr = localtime(<);
- > printf("\nThe system clock shows: %s\n\n",asctime(ptr));
- > debug=strftime(str,SLEN,"The current date is %y-%m-%d",ptr);
- > printf("\nThe value of 'debug' is %d"
- > "\nThe length of 'str' is %d", debug,strlen(str));
- > printf("\nFormatted time string: \"%s\"",str);
- > return debug;
- >}
-
- The only portable values to return from main() are 0, EXIT_SUCCESS and
- EXIT_FAILURE (the last 2 being defined in stdlib.h). return 0 or return
- EXIT_SUCCESS is probably sensible here.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-